package com.ogawa.jopai.devicemanage.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.ogawa.base.utils.imageloader.ImageConfigImpl
import com.ogawa.base.utils.imageloader.ImageLoader
import com.ogawa.base.utils.isEmptyStr
import com.ogawa.base.utils.printNoNull
import com.ogawa.jopai.devicemanage.bean.UserDeviceBean
import com.ogawa.joypal.devicemanage.R
import com.ogawa.projectcommon.utils.ProjectSpUtils

class DeviceManageAdapter: BaseQuickAdapter<UserDeviceBean, BaseViewHolder>(
        R.layout.item_device_manage,
        ArrayList<UserDeviceBean>()
    ) {
    var isVisitor = true
    init {
        val userInfo = ProjectSpUtils.getUserInfo()
        try{
            isVisitor = userInfo.isVisitor
        }catch (e: Exception){
            e.printStackTrace()
        }

    }

    override fun convert(helper: BaseViewHolder, item: UserDeviceBean) {
        helper.addOnClickListener(R.id.itemDeviceManageShareBt,R.id.itemDeviceManageReNameBt,R.id.itemDeviceManageDelBt)
        helper.setText(R.id.itemDeviceTv, printNoNull(item.typeName))
        helper.setText(R.id.itemDeviceLabelTv, printNoNull(item.name))
        ImageLoader.getInstance().loadImage(mContext, ImageConfigImpl.builder()
            .imageView(helper.getView(R.id.itemDeviceIv))
            .isCenterCrop(true)
            .url(item.imageSrc)
            .placeholder(R.drawable.img_chair)
            .build())



        //引用用户设备id(不为空则为接受分享设备)
        if (isEmptyStr(item.quoteUserDeviceId)){
            helper.setGone(R.id.shareIv,false)
            helper
                .setGone(R.id.itemDeviceManageShareBt,true)
                .setGone(R.id.itemDeviceManageReNameBt,true)
                .setGone(R.id.itemDeviceManageDelBt,true)
        }else{
            helper.setGone(R.id.shareIv,true)
            helper
                .setGone(R.id.itemDeviceManageShareBt,false)
                .setGone(R.id.itemDeviceManageReNameBt,false)
                .setGone(R.id.itemDeviceManageDelBt,true)
        }

        //游客不能分享设备
        if (isVisitor){
            helper.setGone(R.id.itemDeviceManageShareBt,false)
        }else{
            if (isEmptyStr(item.quoteUserDeviceId)){
                helper.setGone(R.id.itemDeviceManageShareBt,true)
            }else{
                helper.setGone(R.id.itemDeviceManageShareBt,false)
            }
        }


        if (item.isOnline ==  "0"){
            helper.setText(R.id.itemDeviceStateTv,mContext.getString(R.string.wait_query_status))
                .setTextColor(R.id.itemDeviceStateTv, mContext.resources.getColor(R.color.color68))
        }else if (item.isOnline ==  "01"){
            helper.setText(R.id.itemDeviceStateTv,mContext.getString(R.string.online))
                .setTextColor(R.id.itemDeviceStateTv, mContext.resources.getColor(R.color.color3FC5))

        }else if (item.isOnline ==  "02"){
            helper.setText(R.id.itemDeviceStateTv,mContext.getString(R.string.off_line))
                .setTextColor(R.id.itemDeviceStateTv, mContext.resources.getColor(R.color.color68))

        }else if (item.isOnline ==  "03"){
            helper.setText(R.id.itemDeviceStateTv,mContext.getString(R.string.fail))
                .setTextColor(R.id.itemDeviceStateTv, mContext.resources.getColor(R.color.color68))

        }


    }
}